﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;

namespace WebHost
{
    public class ControlSession
    {
        public const int FROM_MOBILE = 1;
        public const int FROM_PC = 2;

        AutoResetEvent requestEndEvent;
        AutoResetEvent requestStartEvent;

        public ControlSession(int uid)
        {
            UID = uid;
            PPTSideResponse = null;
            ControllerRequest = null;

            requestStartEvent = new AutoResetEvent(false);
            requestEndEvent = new AutoResetEvent(false);
        }

        public AutoResetEvent RequestStartEvent
        {
            get { return requestStartEvent; }
        }

        public AutoResetEvent RequestEndEvent
        {
            get { return requestEndEvent; }
        }

        public EndpointData PPTSideResponse
        {
            get;
            set;

        }

        public EndpointData ControllerRequest
        {
            get;
            set;

        }

        public int UID
        {
            get;
            set;
        }

        public string RequestContentData
        {
            get
            {
                if (ControllerRequest != null)
                    return ControllerRequest.GetContent();
                else
                    throw new Exception("控制端尚未登录");
            }

            set
            {
                if (ControllerRequest != null)
                    ControllerRequest.SetContent(value);
                else
                    throw new Exception("控制端尚未登录");
            }
        }

        public string ResponseContentData
        {
            get
            {
                if (PPTSideResponse != null)
                    return PPTSideResponse.GetContent();
                else
                    throw new Exception("受控端尚未登录");
            }

            set
            {
                if (PPTSideResponse != null)
                    PPTSideResponse.SetContent(value);
                else
                    throw new Exception("受控端尚未登录");
            }
        }

        public void OnUserLogin(int sourceType)
        {
            if (sourceType == FROM_MOBILE && ControllerRequest == null)
                ControllerRequest = new EndpointData();
            else if (sourceType == FROM_PC && PPTSideResponse == null)
                PPTSideResponse = new EndpointData();
        }

        public static ControlSession Create(int uid, int sourceType)
        {
            ControlSession controlSession = new ControlSession(uid);
            EndpointData endPointData = new EndpointData();
            if (sourceType == FROM_MOBILE)
                controlSession.ControllerRequest = endPointData;
            else if (sourceType == FROM_PC)
                controlSession.PPTSideResponse = endPointData;
            return controlSession;
        }
    }
}